package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.*;
import cn.wolfcode.shop.exception.GoodException;
import cn.wolfcode.shop.service.IProductService;
import cn.wolfcode.shop.service.IProductSkuService;
import cn.wolfcode.shop.service.IShopCarService;
import cn.wolfcode.shop.service.IUserLoginService;
import cn.wolfcode.util.RedisConstants;
import com.alibaba.dubbo.config.annotation.Service;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.MessageFormat;

@Service
public class ShopCarServiceImpl implements IShopCarService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IProductSkuService productSkuService;
    @Autowired
    private IProductService productService;

    @Override
    public ShopCar addShopCar(String token, Long productSkuId, BigDecimal number) {
        //判断token是否为空, isEmpty
        if (StringUtils.isEmpty(token)) {

            throw new GoodException("参数有误");
        }
        //不为空:获取redis中的
        String userKey = MessageFormat.format(RedisConstants.USER_TOKEN, token);
        UserLogin userLogin = (UserLogin) redisTemplate.opsForValue().get(userKey);
        if (userLogin == null) {
            throw new GoodException("登录信息过期,请登录后再操作");
        }
        //获取redis中的shopCarKey
        String shopCarKey = MessageFormat.format(RedisConstants.SHOPCAR_ITEM, userLogin.getId(), productSkuId);
        ShopCar shopCar = (ShopCar) redisTemplate.opsForValue().get(shopCarKey);
        if (shopCar == null) {
            //如果shopCar为空,封装购物车相关信息
            shopCar = new ShopCar();
            shopCar.setNumber(number);

            //获取product
            ProductSku productSku = productSkuService.get(productSkuId);
            Product product = productService.getProductById(productSku.getProductId());
            shopCar.setProductSkuId(productSku.getId());
            shopCar.setProductName(product.getName());
            shopCar.setPrice(productSku.getPrice());
            shopCar.setProductImage(product.getImage());

            //拼接字符串: skuPropertyString
            StringBuilder sb = new StringBuilder(50);
            for (ProductSkuProperty psp : productSku.getProductSkuPropertyList()) {
                sb.append(psp.getSkuProperty().getName()).append(":").append(psp.getValue() + " ");
            }
            shopCar.setSkuPropertyString(sb.toString());


            //参数1: 购物车清单 , 参数2:哪个登录用户
            String shopCarListKey = MessageFormat.format(RedisConstants.SHOPCAR_LIST, userLogin.getId());

            //添加到list中的
            redisTemplate.opsForList().leftPush(shopCarListKey, productSkuId);

        } else {
            //不为空, 设添加数量
            shopCar.setNumber(shopCar.getNumber().add(number));
        }
        //存入到redis
        redisTemplate.opsForValue().set(shopCarKey, shopCar);

        return shopCar;
    }
}
